<template>
  <div class="profile-container">
    <!-- 顶部用户信息 -->
    <div class="user-header">
      <div class="user-avatar">
        <img :src="userInfo.avatar || defaultAvatar" alt="用户头像" />
      </div>
      <div class="user-info">
        <h3>{{ userInfo.nickName || '用户' + userInfo.userName }}</h3>
        <p>{{ userInfo.phonenumber || '未绑定手机号' }}</p>
      </div>
      <div class="user-edit" @click="editProfile">
        <i class="el-icon-edit"></i>
      </div>
    </div>

    <!-- 用户统计 -->
    <div class="user-stats">
      <div class="stat-item">
        <div class="stat-number">{{ favoritesCount }}</div>
        <div class="stat-label">收藏</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">{{ viewHistoryCount }}</div>
        <div class="stat-label">浏览</div>
      </div>
      <div class="stat-item">
        <div class="stat-number">{{ publishCount }}</div>
        <div class="stat-label">发布</div>
      </div>
    </div>

    <!-- 功能列表 -->
    <div class="function-list">
      <div class="function-section">
        <div class="section-title">房源管理</div>
        <div class="function-item" @click="goToMyFavorites">
          <i class="el-icon-star-on"></i>
          <span>我的收藏</span>
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="function-item" @click="goToViewHistory">
          <i class="el-icon-time"></i>
          <span>浏览历史</span>
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="function-item" @click="goToMyPublish">
          <i class="el-icon-document"></i>
          <span>我的发布</span>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>

      <div class="function-section">
        <div class="section-title">账户设置</div>
        <div class="function-item" @click="editProfile">
          <i class="el-icon-user"></i>
          <span>个人资料</span>
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="function-item" @click="changePassword">
          <i class="el-icon-lock"></i>
          <span>修改密码</span>
          <i class="el-icon-arrow-right"></i>
        </div>
        <div class="function-item" @click="aboutApp">
          <i class="el-icon-info"></i>
          <span>关于我们</span>
          <i class="el-icon-arrow-right"></i>
        </div>
      </div>

      <div class="function-section">
        <div class="logout-section">
          <el-button type="danger" class="logout-btn" @click="logout" :loading="loading">
            <i class="el-icon-switch-button"></i>
            退出登录
          </el-button>
        </div>
      </div>
    </div>

    <!-- 底部导航 -->
    <footer class="footer-nav">
      <div class="nav-item" @click="goToHome">
        <i class="el-icon-s-home"></i>
        <span>首页</span>
      </div>
      <div class="nav-item" @click="goToPropertyList">
        <i class="el-icon-search"></i>
        <span>找房</span>
      </div>
      <div class="nav-item" @click="goToNews">
        <i class="el-icon-news"></i>
        <span>资讯</span>
      </div>
      <div class="nav-item active">
        <i class="el-icon-user"></i>
        <span>我的</span>
      </div>
    </footer>
  </div>
</template>

<script>
import { getUserProfile } from '@/api/system/user'
import defaultAvatar from '@/assets/images/profile.jpg'

export default {
  name: 'UserProfile',
  data() {
    return {
      userInfo: {},
      defaultAvatar,
      favoritesCount: 12,
      viewHistoryCount: 28,
      publishCount: 3,
      loading: false
    }
  },
  created() {
    this.getUserInfo()
  },
  methods: {
    getUserInfo() {
      getUserProfile().then(response => {
        this.userInfo = response.data
      })
    },
    editProfile() {
      this.$router.push('/user/profile')
    },
    changePassword() {
      this.$router.push('/user/profile?tab=resetPwd')
    },
    goToMyFavorites() {
      this.$message.info('我的收藏功能开发中...')
    },
    goToViewHistory() {
      this.$message.info('浏览历史功能开发中...')
    },
    goToMyPublish() {
      this.$message.info('我的发布功能开发中...')
    },
    aboutApp() {
      this.$message.info('智慧房产销售系统 v1.0.0')
    },
    logout() {
      this.$confirm('确定要退出登录吗？', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.loading = true
        this.$store.dispatch('LogOut').then(() => {
          this.$message.success('已退出登录')
          this.$router.push('/login')
        }).finally(() => {
          this.loading = false
        })
      }).catch(() => {})
    },
    goToHome() {
      this.$router.push('/')
    },
    goToPropertyList() {
      this.$router.push('/property')
    },
    goToNews() {
      this.$message.info('资讯功能开发中...')
    }
  }
}
</script>

<style scoped>
.profile-container {
  min-height: 100vh;
  background-color: #f5f5f5;
  padding-bottom: 60px;
}

.user-header {
  background: linear-gradient(135deg, #667eea 0%, #764ba2 100%);
  color: white;
  padding: 40px 20px;
  display: flex;
  align-items: center;
  position: relative;
}

.user-avatar {
  width: 70px;
  height: 70px;
  border-radius: 50%;
  overflow: hidden;
  border: 3px solid rgba(255, 255, 255, 0.3);
}

.user-avatar img {
  width: 100%;
  height: 100%;
  object-fit: cover;
}

.user-info {
  margin-left: 20px;
  flex: 1;
}

.user-info h3 {
  margin: 0 0 5px 0;
  font-size: 20px;
  font-weight: 600;
}

.user-info p {
  margin: 0;
  opacity: 0.9;
  font-size: 14px;
}

.user-edit {
  position: absolute;
  right: 20px;
  top: 50%;
  transform: translateY(-50%);
  cursor: pointer;
  font-size: 18px;
}

.user-stats {
  display: flex;
  background: white;
  margin: 10px;
  border-radius: 10px;
  padding: 20px 0;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.1);
}

.stat-item {
  flex: 1;
  text-align: center;
  border-right: 1px solid #eee;
}

.stat-item:last-child {
  border-right: none;
}

.stat-number {
  font-size: 24px;
  font-weight: bold;
  color: #667eea;
  margin-bottom: 5px;
}

.stat-label {
  font-size: 14px;
  color: #666;
}

.function-list {
  margin: 10px;
}

.function-section {
  background: white;
  border-radius: 10px;
  margin-bottom: 10px;
  overflow: hidden;
}

.section-title {
  padding: 15px;
  font-size: 16px;
  font-weight: bold;
  color: #333;
  border-bottom: 1px solid #f0f0f0;
}

.function-item {
  display: flex;
  align-items: center;
  padding: 15px;
  border-bottom: 1px solid #f0f0f0;
  cursor: pointer;
  transition: background-color 0.3s;
}

.function-item:hover {
  background-color: #f9f9f9;
}

.function-item:last-child {
  border-bottom: none;
}

.function-item i:first-child {
  font-size: 20px;
  margin-right: 15px;
  color: #667eea;
}

.function-item span {
  flex: 1;
  font-size: 15px;
}

.function-item i:last-child {
  color: #ccc;
  font-size: 16px;
}

.logout-section {
  padding: 20px;
}

.logout-btn {
  width: 100%;
  height: 45px;
  font-size: 16px;
  border-radius: 8px;
}

.footer-nav {
  position: fixed;
  bottom: 0;
  left: 0;
  right: 0;
  background: white;
  display: flex;
  border-top: 1px solid #eee;
  height: 50px;
}

.nav-item {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  color: #666;
  font-size: 12px;
}

.nav-item.active {
  color: #667eea;
}

.nav-item i {
  font-size: 20px;
  margin-bottom: 2px;
}

@media screen and (max-width: 768px) {
  .user-header {
    padding: 30px 15px;
  }
  
  .user-avatar {
    width: 60px;
    height: 60px;
  }
  
  .user-info h3 {
    font-size: 18px;
  }
}
</style>